package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.entity.RtCertBaseCertDO;
import cqrtplm.entity.RtChangeaffectAssociatedbomDO;
import cqrtplm.mapper.RTPartMapper;
import cqrtplm.service.RTChangeRelatedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@Api(tags = "更改请求、变更通告、更改任务相关")
@RequestMapping(RouteConstant.CHANGE)
public class RTChangeRelatedController {

    @Resource
    private RTPartMapper rtPartMapper;
    @Resource
    private RTChangeRelatedService rtChangeRelatedService;

    @GetMapping("ECR/getDrawingNum")
    @ApiOperation(value = "getDrawingNum", notes = "模糊匹配图样代号", produces = "application/json; charset=utf-8")
    public ResponseResult getDrawingNum(String number) {
        List list = new ArrayList<>();
        List<JSONObject> parts = this.rtPartMapper.queryPartsByTypeAndNum(number, "PatternCode");
        for (JSONObject part : parts) {
            list.add(part.getString("CODE"));
        }
        return ResponseResult.success(list);
    }

    @GetMapping("ECR/getCustomerByCode")
    @ApiOperation(value = "getCustomerByCode", notes = "根据图样代号或物料码查询受影响的客户", produces = "application/json; charset=utf-8")
    public ResponseResult getCustomerByCode(String number, String type) throws Exception {
        List<JSONObject> jsonObjectList = this.rtChangeRelatedService.getCustomerByCode(number, type);
        return ResponseResult.success(jsonObjectList);
    }

    @GetMapping("ECR/getProductArea")
    @ApiOperation(value = "getProductArea", notes = "根据图样代号或物料码查询成品码产地", produces = "application/json; charset=utf-8")
    public ResponseResult getProductArea(String number, String type) throws Exception {
        List<JSONObject> jsonObjectList = this.rtChangeRelatedService.getProductArea(number, type);
        return ResponseResult.success(jsonObjectList);
    }

    @GetMapping("ECR/getAssociationDoc")
    @ApiOperation(value = "getAssociationDoc", notes = "根据图样代号查询关联的图文档", produces = "application/json; charset=utf-8")
    public ResponseResult getAssociationDoc(String number) throws Exception {
        List<JSONObject> jsonObjectList = this.rtChangeRelatedService.getAssociationDoc(number);
        return ResponseResult.success(jsonObjectList);
    }

    @PostMapping("getChangeImpactAnalysis")
    @ApiOperation(value = "getChangeImpactAnalysis", notes = "获取受影响的信息", produces = "application/json; charset=utf-8")
    public ResponseResult getChangeImpactAnalysis(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        JSONObject result = this.rtChangeRelatedService.getChangeImpactAnalysis(map);
        return ResponseResult.success(result);
    }

    @PostMapping("saveChangeImpactAnalysis")
    @ApiOperation(value = "saveChangeImpactAnalysis", notes = "保存受影响的信息", produces = "application/json; charset=utf-8")
    public ResponseResult saveChangeImpactAnalysis(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        String msg = this.rtChangeRelatedService.saveChangeImpactAnalysis(map);
        return ResponseResult.success(msg);
    }

    @GetMapping("getRelateChangeDetail")
    @ApiOperation(value = "getRelateChangeDetail", notes = "获取关联变更详情", produces = "application/json; charset=utf-8")
    public ResponseResult getRelateChangeDetail(String changeNo, String partNo) throws Exception {
        JSONObject result = this.rtChangeRelatedService.getRelateChangeDetail(changeNo, partNo);
        return ResponseResult.success(result);
    }

    @GetMapping("getOrderInquiry")
    @ApiOperation(value = "getOrderInquiry", notes = "查询订单项次", produces = "application/json; charset=utf-8")
    public ResponseResult getOrderInquiry(String partNo) throws Exception {
        List<JSONObject> result = this.rtChangeRelatedService.getOrderInquiry(partNo);
        return ResponseResult.success(result);
    }

    @GetMapping("queryAllAffectBOM")
    @ApiOperation(value = "queryAllAffectBOM", notes = "查询所有受影响的BOM", produces = "application/json; charset=utf-8")
    public ResponseResult queryAllAffectBOM(String partNo, String type) throws Exception {
        List<JSONObject> result = this.rtChangeRelatedService.queryAllAffectBOM(partNo, type);
        return ResponseResult.success(result);
    }

    @GetMapping("querySaveAffectBOM")
    @ApiOperation(value = "querySaveAffectBOM", notes = "查询保存的受影响的BOM", produces = "application/json; charset=utf-8")
    public ResponseResult querySaveAffectBOM(String changeNo, String partNo, String segmNum, String partType, String changeStyle) throws Exception {
        List<RtChangeaffectAssociatedbomDO> result = this.rtChangeRelatedService.querySaveAffectBOM(changeNo, partNo, segmNum, partType, changeStyle);
        return ResponseResult.success(result);
    }

    @PostMapping("saveChangeImpactBOM")
    @ApiOperation(value = "saveChangeImpactBOM", notes = "保存受影响的BOM", produces = "application/json; charset=utf-8")
    public ResponseResult saveChangeImpactBOM(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        String msg = this.rtChangeRelatedService.saveChangeImpactBOM(map);
        return ResponseResult.success(msg);
    }

    @PostMapping("synchronousAssociationObj")
    @ApiOperation(value = "synchronousAssociationObj", notes = "同步关联对象", produces = "application/json; charset=utf-8")
    public ResponseResult synchronousAssociationObj(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        String msg = this.rtChangeRelatedService.synchronousAssociationObj(map);
        return ResponseResult.success(msg);
    }

    @GetMapping("collectModels")
    @ApiOperation(value = "collectModels", notes = "机型搜集", produces = "application/json; charset=utf-8")
    public ResponseResult collectModels(String changeNo) throws Exception {
        JSONObject result = this.rtChangeRelatedService.collectModels(changeNo);
        return ResponseResult.success(result);
    }

    @GetMapping("getDevCodeAndModel")
    @ApiOperation(value = "getDevCodeAndModel", notes = "获取机型开发令", produces = "application/json; charset=utf-8")
    public ResponseResult getDevCodeAndModel(String devCode) throws Exception {
        List<JSONObject> result = this.rtChangeRelatedService.getDevCodeAndModel(devCode);
        return ResponseResult.success(result);
    }

    @GetMapping("getMaterialOrDrawNo")
    @ApiOperation(value = "getMaterialOrDrawNo", notes = "获取编码或图样代号", produces = "application/json; charset=utf-8")
    public ResponseResult getMaterialOrDrawNo(String code, String type) throws Exception {
        List<JSONObject> result = this.rtChangeRelatedService.getMaterialOrDrawNo(code, type);
        return ResponseResult.success(result);
    }

    @PostMapping("queryAffectBOM")
    @ApiOperation(value = "queryAffectBOM", notes = "任一条件查询关联变更受影响的BOM", produces = "application/json; charset=utf-8")
    public ResponseResult queryAffectBOM(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeRelatedService.queryAffectBOM(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @PostMapping("querySaveAffectBOM")
        @ApiOperation(value = "querySaveAffectBOM", notes = "获取保存的关联变更受影响的BOM", produces = "application/json; charset=utf-8")
    public ResponseResult querySaveAffectBOM(@RequestBody LinkedHashMap<String, Object> map) throws Exception {
        try {
            return ResponseResult.success(this.rtChangeRelatedService.querySaveAffectBOM(map));
        } catch (Exception e) {
            return ResponseResult.fail("200", e.getMessage());
        }
    }

    @GetMapping("getPageForMaterialOrDrawNo")
    @ApiOperation(value = "getPageForMaterialOrDrawNo", notes = "分页获取编码或图样代号", produces = "application/json; charset=utf-8")
    public ResponseResult getPageForMaterialOrDrawNo(String code, String type,@RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "20") int pageSize) throws Exception {
        PageHelper.startPage(page, pageSize);
        List<JSONObject> result = this.rtChangeRelatedService.getMaterialOrDrawNo(code, type);
        PageInfo<JSONObject> pageInfo =new PageInfo<>(result);
        return ResponseResult.success(pageInfo);
    }

    @GetMapping("queryPartNumberByType")
    @ApiOperation(value = "queryPartNumberByType", notes = "根据编码和类型获取零部件编码", produces = "application/json; charset=utf-8")
    public ResponseResult queryPartNumberByType(String code, String type) throws Exception {
        List<JSONObject> result = this.rtChangeRelatedService.queryPartNumberByType(code, type);
        return ResponseResult.success(result);
    }

}